﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Practices.Unity;

namespace EnCor.ServiceLocator
{
    public class UnityServiceContainer : IServiceContainer
    {
        private IUnityContainer _Container;
        #region IServiceContainer Members

        public UnityServiceContainer()
        {
            _Container = new UnityContainer();
        }

        public UnityServiceContainer(IUnityContainer container)
        {
            _Container = container;
        }

        public void RegisterService<TService>(TService service)
        {
            _Container.RegisterInstance<TService>(service);
        }

        public TService GetService<TService>()
        {
            return _Container.Resolve<TService>();
        }

        public IServiceContainer CreateChildContainer()
        {
            return new UnityServiceContainer( _Container.CreateChildContainer());
        }

        public void RegisterPropertyInjection(Type targetType, string propertyName)
        {
            _Container.Configure<InjectedMembers>()
                .ConfigureInjectionFor(targetType, 
                new InjectionProperty(propertyName));
        }

        public object GetService(Type t)
        {
            return _Container.Resolve(t);
        }

        public void RegisterService(Type t, object instance)
        {
            _Container.RegisterInstance(t, instance);
        }

        #endregion
    }
}
